package org.jdsm.flow.gxt.users.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;
import org.jdsm.flow.gxt.users.client.UserAutoBeanFactory;
import org.jdsm.flow.gxt.users.client.widgets.user.UserGroupGrid;
import org.jdsm.flow.gxt.users.client.widgets.user.UserRoleGrid;
import org.jdsm.flow.gxt.users.client.widgets.user.UsersGrid;

import static org.jdsm.flow.gxt.users.client.UserAutoBeanFactory.User;

/**
 * Created with IntelliJ IDEA.
 * User: mikom
 * Date: 10/24/12
 * Time: 4:02 PM
 */
public class UserLayoutWidget implements IsWidget {

    private UsersGrid usersGrid;
    private UserRoleGrid userRoleGrid;
    private UserGroupGrid userGroupGrid;

    private User selectedUser = null;

    private String LIST_OF_USERS = "Users";
    private String ROLES_FOR_USER = "Roles";
    private String GROUPS_FOR_ROLES = "Groups";

    public UserLayoutWidget(String usersPath, String roleFullPath, String roleCurrentPath, String saveUserRolesPath,
                            String groupFullPath, String groupCurrentPath, String saveUserGroupsPath) {
        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);
        usersGrid = new UsersGrid(usersPath);
        userRoleGrid = new UserRoleGrid(factory, roleFullPath, roleCurrentPath, saveUserRolesPath);
        userGroupGrid = new UserGroupGrid(factory, groupFullPath, groupCurrentPath, saveUserGroupsPath);

    }

    private void buildUsersTab(TabPanel advanced) {
        final PagingToolBar pagingToolBar = new PagingToolBar(5);

        VerticalLayoutContainer cont = new VerticalLayoutContainer();

        cont.add(usersGrid, new VerticalLayoutContainer.VerticalLayoutData(1, 1));
        cont.add(pagingToolBar, new VerticalLayoutContainer.VerticalLayoutData(1, -1));

        VerticalPanel vp = new VerticalPanel();
        vp.add(usersGrid);
        vp.add(pagingToolBar);


        PagingLoader<? extends PagingLoadConfig, ?> loader = usersGrid.getLoader();
        pagingToolBar.bind(loader);
        loader.load();


        advanced.add(cont, LIST_OF_USERS);
    }

    private void buildUserRolesTab(TabPanel advanced, TabItemConfig config) {
        config.setEnabled(false);
        ToolBar toolBar = new ToolBar();
        VerticalLayoutContainer container = new VerticalLayoutContainer();
        container.setBorders(true);
        container.add(toolBar);

        TextButton saveBtn = new TextButton("save");
        saveBtn.addSelectHandler(new SelectEvent.SelectHandler() {
            @Override
            public void onSelect(SelectEvent selectEvent) {
                userRoleGrid.saveData();
                userRoleGrid.saveAction(selectedUser);
            }
        });

        toolBar.add(saveBtn);
        container.add(userRoleGrid);
        advanced.add(container, config);

    }

    private void buildUserGroupTab(TabPanel advanced, TabItemConfig config) {
        config.setEnabled(false);
        ToolBar toolBar = new ToolBar();
        VerticalLayoutContainer container = new VerticalLayoutContainer();
        container.setBorders(true);
        container.add(toolBar);

        TextButton saveBtn = new TextButton("save");
        saveBtn.addSelectHandler(new SelectEvent.SelectHandler() {
            @Override
            public void onSelect(SelectEvent selectEvent) {
                userGroupGrid.saveData();
                userGroupGrid.saveAction(selectedUser);
            }
        });

        toolBar.add(saveBtn);
        container.add(userGroupGrid);
        advanced.add(container, config);
    }


    @Override
    public Widget asWidget() {
//        FlowPanel vp = new FlowPanel();
//        vp.setSize("100%", "100%");
        VerticalPanel vp = new VerticalPanel();
        vp.setSpacing(10);

        final TabPanel advanced = new TabPanel();
        advanced.setPixelSize(600, 450);
        /*advanced.setAnimScroll(true);
        advanced.setTabScroll(true);
        advanced.setCloseContextMenu(true);*/


        buildUsersTab(advanced);

        final TabItemConfig tabItemConfigForRoles = new TabItemConfig(ROLES_FOR_USER);
        buildUserRolesTab(advanced, tabItemConfigForRoles);


        final TabItemConfig tabItemConfigForGroups = new TabItemConfig(GROUPS_FOR_ROLES);
        buildUserGroupTab(advanced, tabItemConfigForGroups);

//        tabItemConfigForGroups.setEnabled(false);
//        advanced.add(userGroupGrid.asWidget(), tabItemConfigForGroups);
        advanced.addSelectionHandler(new SelectionHandler<Widget>() {
            @Override
            public void onSelection(SelectionEvent<Widget> event) {
                TabPanel panel = (TabPanel) event.getSource();
                Widget w = event.getSelectedItem();
                TabItemConfig config = panel.getConfig(w);
                String tabTitle = config.getText();
                if (tabTitle == null) {
                    return;
                }
                if (tabTitle.equals(ROLES_FOR_USER)) {
                    userRoleGrid.loadDataByUser(selectedUser);
                }
                if (tabTitle.equals(GROUPS_FOR_ROLES)) {
                    userGroupGrid.loadDataByUser(selectedUser);
                }

            }
        });

        usersGrid.getGrid().getSelectionModel().addSelectionChangedHandler(new SelectionChangedEvent.SelectionChangedHandler<User>() {
            @Override
            public void onSelectionChanged(SelectionChangedEvent<User> userSelectionChangedEvent) {
                if (userSelectionChangedEvent.getSelection().size() == 1) {
                    selectedUser = usersGrid.getGrid().getStore().findModel(userSelectionChangedEvent.getSelection().get(0));
                    tabItemConfigForRoles.setEnabled(true);
                    tabItemConfigForGroups.setEnabled(true);
                    advanced.forceLayout();
                } else {
                    usersGrid.getGrid().getSelectionModel().deselect(selectedUser);
                    selectedUser = usersGrid.getGrid().getSelectionModel().getSelectedItem();
                }
            }
        });


//        advanced.setActiveWidget(advanced.getWidget(6));

        vp.add(advanced);

        return vp;
    }
}
